import axios from 'axios'
import { BASE_PROXY, TIMEOUT, AUTH , CODE_REQUEST_BASE, CODE_TOKEN_EXPIRED, CODE_REQUEST_OTHER, CODE_PERMANENT_REDIRECT, CODE_REQUESD_ERROR, CODE_NOT_FOUND, CODE_SERVER_ERROR, CODE_REQUEST_SUCCESS, NOT_HAVE_PERMISSION } from '@/config'
import {getItem,setItem,clearAll} from '@/utils/storage'
import router from '@/router'


const instance = axios.create({
    baseURL: import.meta.env.VITE_APP_MODE == 'development' ? BASE_PROXY : import.meta.env.VITE_APP_BASE_API,
    timeout: TIMEOUT,
})

instance.interceptors.request.use(config => {
    const { headers } = config;
    getItem('token') && Object.assign(headers, {
        [AUTH]: getItem('token'),
    })
    return config
}, err => {
    return Promise.reject(err)
})

instance.interceptors.response.use(res => {
     // 错误处理
     switch (res.data.code) {
        case CODE_REQUEST_BASE:
            // ElMessage({
            //     type: 'warning',
            //     message: res.data.msg,
            //     duration: 5 * 1000,
            //     showClose: true,
            //     grouping: true,
            //     customClass: 'messageClass'
            // })
            break;
        case NOT_HAVE_PERMISSION:
            // ElMessage({
            //     type: 'warning',
            //     message: res.data.msg,
            //     duration: 5 * 1000,
            //     showClose: true,
            //     grouping: true,
            //     customClass: 'messageClass'
            // })
            break;
        case CODE_PERMANENT_REDIRECT:
            // ElMessage({
            //     type: 'error',
            //     message: '永久重定向，请求的资源已被分配了新的 URL',
            //     duration: 5 * 1000,
            //     showClose: true,
            //     grouping: true,
            //     customClass: 'messageClass'
            // })
            break;
        case CODE_REQUESD_ERROR:
            // ElMessage({
            //     type: 'error',
            //     message: '客户端发送的请求有错误，服务器无法处理此请求',
            //     duration: 5 * 1000,
            //     showClose: true,
            //     grouping: true,
            //     customClass: 'messageClass'
            // })
            break;
        case CODE_NOT_FOUND:
            // ElMessage({
            //     type: 'error',
            //     message: '请求的资源不存在',
            //     duration: 5 * 1000,
            //     showClose: true,
            //     grouping: true,
            //     customClass: 'messageClass'
            // })
            break;
        case CODE_TOKEN_EXPIRED:
            // ElMessage({
            //     type: 'warning',
            //     message: res.data.msg,
            //     duration: 5 * 1000,
            //     showClose: true,
            //     grouping: true,
            //     customClass: 'messageClass'
            // })
            // token过期--> 跳回登录
            router.push({ path: '/login' });
            clearAll()
            break;
        case CODE_SERVER_ERROR:
            // ElMessage({
            //     type: 'error',
            //     message: res.data.msg,
            //     duration: 5 * 1000,
            //     showClose: true,
            //     grouping: true,
            //     customClass: 'messageClass'
            // })
            break;
        default:
            break;
    }
    return Promise.resolve(res.data)
}, err => {
    return Promise.reject(err)
})

const request = {}
['get', 'post', 'put', 'delete'].forEach(method => {
    request[method] = function (url, data, header) {
        if (method == 'get') {
            return instance.get(url, {
                params: { ...data },
                headers: { ...header }
            })
        } else if (method == 'post') {
            return instance.post(url, data, { header })
        }
    }
})
